const Joi = require("joi")

// 确定好规则
const username = Joi.string().min(1).required().messages({
    'string.base': `"username" should be a type of 'text'`,
    'string.alphanum': `"username" should only contain letters and numbers`,
    'string.min': `"username" should have a minimum length of {#limit}`,
    'any.required': `"username" is a required field`
})
const password = Joi.string().min(1).required().messages({
    'string.base': `"password" should be a type of 'text'`,
    'any.required': `"password" is a required field`
})
const againPwd = Joi.string().min(1).required().messages({
    'string.base': `"againPwd" should be a type of 'text'`,
    'any.required': `"againPwd" is a required field`
})
const type = Joi.number().min(1).required().messages({
    'number.base': `"type" should be a type of 'number'`,
    'any.required': `"type" is a required field`
})
const phone = Joi.string().pattern(/^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/).required().messages({
    'string.base': `"phone" should be a type of 'text'`,
    'any.required': `"phone" is a required field`
})
const u_id = Joi.string().min(1).required().messages({
    'string.base': `"u_id" should be a type of 'text'`,
    'any.required': `"u_id" is a required field`
})

const nick_name = Joi.string().min(1).required().messages({
    'string.base': `"nick_name" should be a type of 'text'`,
    'any.required': `"nick_name" is a required field`
})
const avatar_url = Joi.string().min(1).required().messages({
    'string.base': `"avatar_url" should be a type of 'text'`,
    'any.required': `"avatar_url" is a required field`,
})
const gender = Joi.number().min(1).required().messages({
    'number.base': `"gender" should be a type of 'number'`,
    'any.required': `"gender" is a required field`
})






const loginSchema = Joi.object({
    username,
    password,
  type,
})
const registerSchema = Joi.object({
    username,
    password,
    type,
    phone,    
    againPwd,
})

const userInfoSchema = Joi.object({
    u_id,
})

const updateInfoSchema = Joi.object({
    u_id,
    nick_name,
    avatar_url,
    gender,
    phone,
})

const wxLoginSchema = Joi.object({
    code: Joi.string().min(1).required().messages({
        'string.base': `"code" should be a type of 'text'`,
        'any.required': `"code" is a required field`
    }),
    encryptedData: Joi.string().min(1).required().messages({
        'string.base': `"encryptedData" should be a type of 'text'`,
        'any.required': `"encryptedData" is a required field`

    }),
    iv: Joi.string().min(1).required().messages({
        'string.base': `"iv" should be a type of 'text'`,
        'any.required': `"iv" is a required field"`
    }),
    type,
    userInfo: Joi.string().min(1).required().messages({
        'string.base': `"userInfo" should be a type of 'text'`,
        'any.required': `"userInfo" is a required field"`
    }),
})

const userRolesSchema = Joi.object({
    u_id,
})

module.exports = {
    loginSchema,
    registerSchema,
    userInfoSchema,
    updateInfoSchema,
    wxLoginSchema,
    userRolesSchema,
}